package net.oschina.app.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import net.oschina.app.R;
import net.oschina.app.adapter.TabVPAdpater;

/**
 * Created by YL on 2017/4/2.
 */

public abstract class BaseTabPageFragment extends Fragment {
    private TabVPAdpater adpater;
    private android.support.design.widget.TabLayout commontab;
    private android.support.v4.view.ViewPager commonvp;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View base_tabp_view = inflater.inflate(R.layout.base_tab_viewpage_fragment, container, false);
        this.commonvp = (ViewPager) base_tabp_view.findViewById(R.id.common_vp);
        this.commontab = (TabLayout) base_tabp_view.findViewById(R.id.common_tab);
        //3.创建ViewPager数据适配器   PagerAdapter
        adpater = new TabVPAdpater(getContext(), getChildFragmentManager());
        //4. 将数据适配器设置给ViewPager
        commonvp.setAdapter(adpater);
        //关联
        commontab.setupWithViewPager(commonvp);
        //6. 初始化页签信息 :与数据相关 :页签的标题
        setupTab(adpater);
        //设置第一个显示的页面
        commonvp.setCurrentItem(getFirstItem());
        return base_tabp_view;
    }

    protected abstract int getFirstItem();

    public Bundle getBundle(String arg) {
        Bundle bundle = new Bundle();
        bundle.putString("key", arg);
        return bundle;
    }

    //由于基类不能决定子类中的标签设置 ，所以抽象出去，由子类来实现
    public abstract void setupTab(TabVPAdpater adpater);
}
